package util::time;

require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(stamp2date stamp2time stamp2sec date2stamp time2stamp);


sub stamp2date
{
	my $stamp = shift;
	my $formal = shift || 0;

	my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
            localtime($stamp);
    my $date;
    if ($formal == 1)
    {
		$date = sprintf "%04d%02d%02d",$year + 1900, $mon + 1, $mday;
    }
    elsif ($formal == 2)
    {
    	$date = sprintf "%04d_%02d_%02d",$year + 1900, $mon + 1, $mday;
    }
    else
    {
    	$date = sprintf "%04d-%02d-%02d",$year + 1900, $mon + 1, $mday;
    }
	return $date;        
}

sub stamp2time
{
	my $stamp = shift;
	my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
            localtime($stamp);
	my $time = sprintf "%02d:%02d:00",$hour,$min;
	return $time;
}

sub stamp2sec
{
	my $stamp = shift;
	my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
            localtime($stamp);
#    my $second = $sec;
    return $sec;
}

sub date2stamp
{
	use Time::Local;
	my $date = shift;
	my ($year,$mon,$mday) = split m/-/, $date;
	my ($sec,$min,$hour) = (0,0,0);
	#print "$year,$mon,$day";
	my $stamp = timelocal($sec,$min,$hour,$mday,$mon,$year);
	return $stamp;
}

sub time2stamp
{
	use Time::Local;
	my $time = shift;
	my ($mday,$mon,$year) = (localtime(time))[3,4,5];
	my ($hour, $min, $sec) = split m/:/, $time;
	my $stamp = timelocal($sec,$min,$hour,$mday,$mon,$year);
	return $stamp;
}

1;